package com.app.aipass.fragment.guadandetail;

import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.databinding.ObservableField;

import com.app.aipass.R;
import com.app.aipass.entity.TransactionPlanEntity;
import com.app.aipass.fragment.orderdetail.OrderDetailFragment;
import com.app.aipass.utils.Utils;

import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;

/**
 * @author: woking
 * @date: 2021/6/17
 * @description：
 */
public class GuaDanDetailItemViewModel extends ItemViewModel<GuaDanDetailViewModel> {
    public ObservableField<TransactionPlanEntity.OrdersData> entity = new ObservableField<>();
    public Drawable drawableImg;

    public GuaDanDetailItemViewModel(@NonNull GuaDanDetailViewModel viewModel, TransactionPlanEntity.OrdersData entity) {
        super(viewModel);
        this.entity.set(entity);
        //ImageView的占位图片，可以解决RecyclerView中图片错误问题
        drawableImg = ContextCompat.getDrawable(viewModel.getApplication(), R.mipmap.ic_launcher);
    }

    /**
     * 获取position的方式有很多种,indexOf是其中一种，常见的还有在Adapter中、ItemBinding.of回调里
     *
     * @return
     */
    public int getPosition() {
        return viewModel.getItemPosition(this);
    }

    //条目的点击事件
    public BindingCommand itemClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if(!Utils.isLogin())
            {
                return;
            }
            Bundle mBundle = new Bundle();
            mBundle.putSerializable("entity", entity.get());
            mBundle.putBoolean("isOrder", false);
            viewModel.startContainerActivity(OrderDetailFragment.class.getCanonicalName(), mBundle);
        }
    });
    public String getType()
    {
        String status = "求购";
        switch (entity.get().type)
        {
            case 1:
                status = "求购";
                break;
            case 2:
                status = "转赠";
                break;
        }
        return status;
    }
    public String getStatus()
    {
        //订单状态:0=待支付,1=待确认,2=确认成功,3=订单关闭,4=仲裁 5 待审核
        //订单状态:0=待付款,1=待放行,2=已完成,3=已取消,4=异常 5 待审核
        String status = "待付款";
        switch (entity.get().status)//status 订单状态:0=进行中,1=完成 2取消 3 待审核
        {
            case 0:
                status = "待付款";
                break;
            case 1:
                status = "待放行";
                break;
            case 2:
                status = "已完成";
                break;
            case 3:
                status = "已取消";
                break;
            case 4:
                status = "异常";
                break;
            case 5:
                status = "待审核";
                break;
        }
        return status;
    }

    //条目的长按事件
    public BindingCommand itemLongClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //以前是使用Messenger发送事件，在NetWorkViewModel中完成删除逻辑
//            Messenger.getDefault().send(NetWorkItemViewModel.this, NetWorkViewModel.TOKEN_NETWORKVIEWMODEL_DELTE_ITEM);
            //现在ItemViewModel中存在ViewModel引用，可以直接拿到LiveData去做删除
//            ToastUtils.showShort(entity.get().getName());
        }
    });
//    /**
//     * 可以在xml中使用binding:currentView="@{viewModel.titleTextView}" 拿到这个控件的引用, 但是强烈不推荐这样做，避免内存泄漏
//     **/
//    private TextView tv;
//    //将标题TextView控件回调到ViewModel中
//    public BindingCommand<TextView> titleTextView = new BindingCommand(new BindingConsumer<TextView>() {
//        @Override
//        public void call(TextView tv) {
//            NetWorkItemViewModel.this.tv = tv;
//        }
//    });
}
